package com.zdsoft.blp.zongbuju.shouye.linliquan.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.isLoginSuccess.IsLoginSuccess;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.gongyong.xlistview.XListView;
import com.zdsoft.blp.zongbuju.shouye.linliquan.adapter.SheQuLunTanAdapter;
import com.zdsoft.blp.zongbuju.shouye.linliquan.adapter.ZZLYadapter;
import com.zdsoft.blp.zongbuju.shouye.linliquan.entity.LinLi;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/31.
 */
public class LLLunTanActivity extends BaseActivity implements NetCallback,XListView.IXListViewListener {
    private XListView xlv;
    private ImageView title_bianji;
    private String name,title,date1,content,zan,pinglun,touxiang,image,id;
    private boolean rows,isImage;
    private Intent intent;
    private SheQuLunTanAdapter adapter;
    private FanHuiView fh_title;
    private List<LinLi>list,mlist;
    private int pageNo=1;

    @Override
    protected void init() {
        setContentView(R.layout.linli_shequluntan);
        xlv = find(R.id.linli_shequluntan_lv);
        xlv.setPullRefreshEnable(true);//设置可以刷新
        xlv.setPullLoadEnable(true);//设置可以加载更多
        xlv.setXListViewListener(this);//添加监听事件
        title_bianji = find(R.id.title_bianji);
        //设置标题
        fh_title = find(R.id.fh_title);
        fh_title.getTv_title().setText("社区论坛");
        //    解析JOSN
        getNet();


        // XListView item点击事件
        xlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int Id = position - 1;
                intent = new Intent(LLLunTanActivity.this, XQLTActivity.class);
                intent.putExtra("name", list.get(Id).getUsername());
                intent.putExtra("title", list.get(Id).getTitle());
                intent.putExtra("date1", list.get(Id).getDate());
                intent.putExtra("content", list.get(Id).getContent());
                intent.putExtra("zan", list.get(Id).getZanNum());
                intent.putExtra("pinglun", list.get(Id).getPingNum());
                intent.putExtra("touxiang", list.get(Id).getTou());
                intent.putExtra("image", list.get(Id).getContentImage());
                startActivity(intent);

            }
        });
        title_bianji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               if (IsLoginSuccess.isloginSuccess(LLLunTanActivity.this,true)){
                    intent = new Intent(LLLunTanActivity.this, FBLTActivity.class);
                    startActivity(intent);
                }

            }
        });

    }
    @Override
    public void onResponse(boolean flag, int code, String res) {
        Log.e("res",res);
        if (flag){
            if (pageNo==1){
                mlist=jiexi(code,res);
                //        listview 展示
                adapter=new SheQuLunTanAdapter(list,LLLunTanActivity.this);
                xlv.setAdapter(adapter);
                xlv.stopRefresh();
            }else {
                list=jiexi(code,res);
                for (LinLi linLis:list){
                    mlist.add(linLis);
                }
                adapter.setListData(mlist);
                xlv.stopLoadMore();
            }
        }
    }

    private List<LinLi> jiexi(int code, String res) {
        list = new ArrayList<>();
        LinLi user =null;
        try {
            JSONObject object = new JSONObject(res);
            rows = object.isNull("rows");
            if (rows == false) {
                JSONArray array = object.getJSONArray("rows");
                for (int i = 0; i < array.length(); i++) {
                    JSONObject obj = array.getJSONObject(i);
                    title = obj.getString("title");
                    content = obj.getString("content");
                    date1 = obj.getString("createTime");
                    name = obj.getString("releaseUser");
                    touxiang = obj.getString("headimgurl");
                    String Praise=obj.getString("isPraise");
                    boolean isPraise = false;
                    if ("N".equals(Praise)){
                            isPraise=false;
                    }else if ("Y".equals(Praise)){
                        isPraise=true;
                    }
                    int approvedResult = obj.getInt("approvedResult");
                    zan = obj.getString("praiseNum");
                    id=obj.getString("id");
                    isImage = obj.isNull("picPath");
                    if (isImage == false) {
                        image = obj.getString("picPath");
                    }
                    pinglun = obj.getString("replyNum");
                    if (code==1){
                        user = new LinLi(name, title, content, zan, pinglun, touxiang, date1, image);
                        user.setCode(1);
                        user.setId(id);
                        user.setZan(isPraise);
                    }
                    if (code==2){
                        user = new LinLi(name, title, content, zan, pinglun, touxiang, date1, image,approvedResult);
                        user.setCode(22);
                    }

                    list.add(user);
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }


    @Override
    public void onRefresh() {
        mlist=new ArrayList<>();
        pageNo=1;
        getNet();
    }

    @Override
    public void onLoadMore() {
        pageNo++;
        getNet();

    }

    public void getNet() {
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        String uerrId = sp.getString("userId","");
        String communityCode = sp.getString("communityCode", "00001");
        if(!"".equals(uerrId)){
        intent=getIntent();
        int source=intent.getIntExtra("source",1);
        Net net=new Net();
        net.nameSpace=API.BBS_NAMESPACE;
        net.url=API.BBS_URL;
        Map<String,Object> map=new HashMap<>();
        map.put("pageNo",pageNo);
        map.put("pageSize",10);
        map.put("userId",uerrId);
        map.put("communityCode",communityCode);
        map.put("condition","");
        map.put("curTime","");
        net.parms=map;
        if (source==2){
            net.code=2;
            net.methodName=API.MYBBSLIST;
        }else {
            net.methodName=API.BBSLIST;
            net.code=1;

        }
        getServer(net,this);
        }
    }
}
